package vis.app.components;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import vis.app.controller.AppController;
import vis.app.controller.EAppState;
import vis.app.utils.Utils;

public class PanelControl extends JPanel implements IPanelComponent {
	private static final long serialVersionUID = 5908898530179785606L;

	JButton btnStart;
	JButton btnStop;

	ImageIcon iconStart;
	ImageIcon iconStop;
	ImageIcon iconPause;
	ImageIcon iconResume;

	@Override
	public void init() {
		loadImageIcons();

		this.setLayout(new BorderLayout());
		initPanelControl();
		initParamControl();

		addButtonListener();
		updateButtonStatus(EAppState.STATE_READY);
	}

	private void loadImageIcons() {
		iconStart = Utils.createImageIcon("/btnStart.png");
		iconStop = Utils.createImageIcon("/btnStop.png");
		iconPause = Utils.createImageIcon("/btnPause.png");
		iconResume = Utils.createImageIcon("/btnResume.png");
	}

	private void initPanelControl() {
		JPanel panelControl = new JPanel();
		panelControl.setPreferredSize(new Dimension(300, 0));

		btnStart = new JButton();
		btnStart.setIcon(iconStart);
		btnStart.setPreferredSize(new Dimension(150, 0));
		btnStop = new JButton();
		btnStop.setIcon(iconStop);
		btnStop.setPreferredSize(new Dimension(150, 0));

		panelControl.setLayout(new BorderLayout());
		panelControl.add(btnStart, BorderLayout.WEST);
		panelControl.add(btnStop, BorderLayout.EAST);

		this.add(panelControl, BorderLayout.WEST);
	}

	private void initParamControl() {
		JPanel panelParam = new JPanel();
		panelParam.setPreferredSize(new Dimension(724, 0));

		JLabel label1 = new JLabel("Param1");
		panelParam.add(label1);
		JTextField textField1 = new JTextField("Param1");
		panelParam.add(textField1);

		JButton btnPush = new JButton("PUSH");
		panelParam.add(btnPush);
		btnPush.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				AppController.getInstance().dispatchAction(AppController.ACTION_PUSH);
			}
		});

		JButton btnPop = new JButton("POP");
		panelParam.add(btnPop);
		btnPop.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				AppController.getInstance().dispatchAction(AppController.ACTION_POP);
			}
		});

		this.add(panelParam, BorderLayout.EAST);
	}

	private void addButtonListener() {
		btnStart.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println("BTN START CLICKED");
				AppController.getInstance().toggleRunningPaused();
				updateButtonStatus(AppController.getInstance().getAppState());
			}
		});

		btnStop.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				System.out.println("BTN STOP CLICKED");
				AppController.getInstance().setStateFinished();
				updateButtonStatus(AppController.getInstance().getAppState());

				// TODO update ready status
			}
		});
	}

	private void updateButtonStatus(EAppState state) {
		switch (state) {
		case STATE_READY:
			btnStart.setEnabled(true);
			btnStart.setIcon(iconStart);
			btnStop.setEnabled(false);
			break;
		case STATE_RUNNING:
			btnStart.setEnabled(true);
			btnStart.setIcon(iconPause);
			btnStop.setEnabled(true);
			btnStop.setIcon(iconStop);
			break;
		case STATE_PAUSED:
			btnStart.setEnabled(true);
			btnStart.setIcon(iconResume);
			btnStop.setEnabled(true);
			btnStop.setIcon(iconStop);
			break;
		case STATE_FINISHED:
			btnStart.setEnabled(true);
			btnStart.setIcon(iconStart);
			btnStop.setEnabled(false);
			break;

		default:
			break;
		}
	}

}
